package com.webtest.fileupload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/8/23 21:39
 */
public class DownLoadServlet extends HttpServlet {
    private static final long serialVersionUID = 4376043532020903841L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/x-msdownload");
        String fileName = "文件下载.ppt";
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));

        OutputStream outputStream = resp.getOutputStream();
        String pptFileNme = "D:\\视频学习资料\\java设计模式系列\\03抽象工厂模式\\03第三讲抽象工厂模式.ppt";

        InputStream inputStream = new FileInputStream(pptFileNme);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }

        inputStream.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
